package com.movie.repository;

import com.movie.entity.UserFavorite;
import com.movie.entity.User;
import com.movie.entity.Movie;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface UserFavoriteRepository extends JpaRepository<UserFavorite, Long> {
    
    // 根据用户ID查找收藏列表
    List<UserFavorite> findByUser_Id(Long userId);
    
    // 根据用户和电影查找收藏记录
    Optional<UserFavorite> findByUserAndMovie(User user, Movie movie);
    
    // 检查用户是否收藏了某部电影
    boolean existsByUser_IdAndMovie_Id(Long userId, Long movieId);
    
    // 根据用户ID和电影ID删除收藏
    void deleteByUser_IdAndMovie_Id(Long userId, Long movieId);

    // 根据用户ID和电影ID列表批量删除收藏
    @Modifying
    @Query("delete from UserFavorite uf where uf.user.id = :userId and uf.movie.id in :movieIds")
    void deleteBatchByUserIdAndMovieIds(@Param("userId") Long userId, @Param("movieIds") List<Long> movieIds);
    
    // 统计用户的收藏数量
    long countByUser_Id(Long userId);
}